const mongoose = require('../db/index').mongoose;
const { productModel } = require('../db/models/cosmetic');
const fs = require('fs');

async function rollback() {
  try {
    console.log('开始回滚 hospital 字段...');
    
    const backupData = JSON.parse(fs.readFileSync('D:\Users\lzy\Desktop\cosmetic-surgery\taro\houduan\scripts\backups\hospital-field-backup-2025-09-12T08-57-23.json', 'utf8'));
    console.log(`从备份文件恢复 ${backupData.products.length} 个产品的 hospital 字段`);
    
    let successCount = 0;
    for (const productData of backupData.products) {
      await productModel.updateOne(
        { _id: productData._id },
        { $set: { hospital: productData.hospital } }
      );
      successCount++;
    }
    
    console.log(`✅ 成功恢复 ${successCount} 个产品的 hospital 字段`);
  } catch (error) {
    console.error('❌ 回滚失败:', error);
  } finally {
    await mongoose.connection.close();
  }
}

rollback();